package com.vnie.mina.message;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vnie.mina.util.Util;


/**
 * 签到交易请求
 * @author HO274541
 *
 */
public class Trancation11Request extends BaseMessage {

	private final static Log log = LogFactory.getLog(Trancation11Request.class);

	public Trancation11Request(){
		super();
		try {
			//公共字段赋值放在基类中
			setTrcode(Util.buildTLV(3, VConstant.T_11));
			setSerial(Util.buildTLV(15, "1234567890"));
		} catch (UnsupportedEncodingException e) {
			log.error(e.getStackTrace());
		}
		
		
	}
	
	public void encode() {
		
		//计算报文长度=报文头长度+报文体长度+MAC长度
		int len=0;
		len+=getTpdu().length;
		len+=getAppcode().length;
		
		len+=getVersion().length;
		len+=getTrcode().length;
		len+=getExmchtid().length;
		len+=getExtermid().length;
		len+=getPayway().length;
		len+=getTrack2().length;
		len+=getAmt().length;
		len+=getPin().length;
		len+=getVender().length;
		len+=getTtype().length;
		len+=getCustid().length;
		len+=getSerial().length;
		//mac长度
		len+=8;
		
		setLen(Util.shortToByte((short)len));
		
		entity = new byte[len+getLen().length];
		//预填充报文
		Arrays.fill(entity, (byte)0x00);
		//构建发送包
		try {
			//构建发送包
			int count = 0;
			System.arraycopy(getLen(), 0, entity, count, getLen().length);
			count+=getLen().length;
			
			System.arraycopy(getTpdu(), 0, entity, count, getTpdu().length);
			count+=getTpdu().length;
			System.arraycopy(getAppcode(), 0, entity, count, getAppcode().length);
			count+=getAppcode().length;
			int start = count;
			System.arraycopy(getVersion(), 0, entity, count, getVersion().length);
			count+=getVersion().length;
			System.arraycopy(getTrcode(), 0, entity, count, getTrcode().length);
			count+=getTrcode().length;
			System.arraycopy(getExmchtid(), 0, entity, count, getExmchtid().length);
			count+=getExmchtid().length;
			System.arraycopy(getExtermid(), 0, entity, count, getExtermid().length);
			count+=getExtermid().length;
			System.arraycopy(getPayway(), 0, entity, count, getPayway().length);
			count+=getPayway().length;
			System.arraycopy(getTrack2(), 0, entity, count, getTrack2().length);
			count+=getTrack2().length;
			System.arraycopy(getAmt(), 0, entity, count, getAmt().length);
			count+=getAmt().length;
			System.arraycopy(getPin(), 0, entity, count, getPin().length);
			count+=getPin().length;
			System.arraycopy(getVender(), 0, entity, count, getVender().length);
			count+=getVender().length;
			System.arraycopy(getTtype(), 0, entity, count, getTtype().length);
			count+=getTtype().length;
			System.arraycopy(getCustid(), 0, entity, count, getCustid().length);
			count+=getCustid().length;
			System.arraycopy(getSerial(), 0, entity, count, getSerial().length);
			count+=getSerial().length;
			//slide出需要计算mac的报文
			byte[] slideEntity = new byte[count-start];
			System.arraycopy(entity, start, slideEntity, 0, count-start);
			//setMac(new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00});
			setMac(Util.buildMac(slideEntity, slideEntity.length));
			System.arraycopy(getMac(), 0, entity, count, getMac().length);
			count+=getMac().length;
			Util.writeLog(entity,"Trancation11Request");
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Trancation11Request request = new Trancation11Request();
//		byte[] test = new byte[]{0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x01,0x02,0x01,0x01,0x01,0x01,0x01,0x09};
		//request.buildMac(test, 16);
		request.encode();
		System.out.println(request.getMac());
		
	}
}
